package com.atguigu.servlet;

import sun.security.mscapi.CPublicKey;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 1。实例化                        构造器
 * 2，初始化                        init
 * 3，接受请求，处理请求，响应结果      service
 * 4，销毁                         destory
 *
 * */

@WebServlet(value = "/servletlifeCycle",loadOnStartup = 100)
public class ServletLifeCycle extends HttpServlet {


    //构造器
    public ServletLifeCycle() {
        //super();
        System.out.println("构造器");
    }
    //初始化
    @Override
    public void init() throws ServletException {
        System.out.println("初始化");
    }

   /* @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("接受请求，处理请求，响应结果");

    }*/

    // 重写get方法
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("接受请求，处理get请求，响应结果");
        PrintWriter out = resp.getWriter();
        out.write("ServletLifeCycle");
    }
    // 重写post方法
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("接受请求，处理post请求，响应结果");
    }

    @Override
    public void destroy() {
        //super.destroy();
        System.out.println("销毁");
    }

    /**
     * Servlet继承结构
     * 1.顶级的Servlet接口：javax.servlet.Servlet
     * 2.顶级的Servlet接口的实现类：javax.servlet.http.HttpServlet
     * 3.顶级的Servlet接口的实现类的子类：javax.servlet.http.HttpServlet
     * 4.子类的子类：javax.servlet.http.HttpServlet
     *  public interface Servlet {
     *  //方法
     *     //初始化方法
     *    void init();
     *    //获取ServletConfig对象的方法
     *    ServletConfig getServletConfig();
     *    //接收用户请求，用于响应的方法
     *    void service();
     *    返回Servlet字符串形式描述
     *    String getServletInfo();
     *    //销毁方法
     *    void destroy();
     * }
     * */
}
